namespace TSL.Utility.Json
{
    using System;
    using System.Data;
    using System.Text;

    public class DataTableConverter : IJavaScriptConverter
    {
        private string clientType = "Ajax.Web.DataTable";

        public DataTableConverter()
        {
            base.m_AllowInheritance = true;
            base.m_serializableTypes = new Type[] { typeof(DataTable) };
            base.m_deserializableTypes = new Type[] { typeof(DataTable) };
        }

        public override object Deserialize(IJavaScriptObject o, Type t)
        {
            JavaScriptObject obj2 = o as JavaScriptObject;
            if (obj2 == null)
            {
                throw new NotSupportedException();
            }
            if (!(((obj2.Contains("Columns") && (obj2["Columns"] is JavaScriptArray)) && obj2.Contains("Rows")) && (obj2["Rows"] is JavaScriptArray)))
            {
                throw new NotSupportedException();
            }
            JavaScriptArray array = (JavaScriptArray) obj2["Columns"];
            JavaScriptArray array2 = (JavaScriptArray) obj2["Rows"];
            DataTable table = new DataTable();
            DataRow row = null;
            if (obj2.Contains("TableName") && (obj2["TableName"] is JavaScriptString))
            {
                table.TableName = obj2["TableName"].ToString();
            }
            int num = 0;
            while (num < array.Count)
            {
                JavaScriptArray array3 = (JavaScriptArray) array[num];
                Type type = Type.GetType(array3[1].ToString(), true);
                table.Columns.Add(array3[0].ToString(), type);
                num++;
            }
            JavaScriptArray array4 = null;
            for (int i = 0; i < array2.Count; i++)
            {
                array4 = (JavaScriptArray) array2[i];
                row = table.NewRow();
                for (num = 0; num < array4.Count; num++)
                {
                    object obj3 = JavaScriptDeserializer.Deserialize(array4[num], table.Columns[num].DataType);
                    row[num] = (obj3 == null) ? DBNull.Value : obj3;
                }
                table.Rows.Add(row);
            }
            return table;
        }

        public override string GetClientScript()
        {
            if (Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                return "";
            }
            return (JavaScriptUtil.GetClientNamespaceRepresentation(this.clientType) + "\r\n" + this.clientType + " = function(c, r) {\r\n\tthis.__type = \"System.Data.DataTable,System.Data\";\r\n\tthis.Columns = [];\r\n\tthis.Rows = [];\r\n\tthis.addColumn = function(name, type) {\r\n\t\tthis.Columns.push({Name:name,__type:type});\r\n\t};\r\n\tthis.toJSON = function() {\r\n\t\tvar dt = {};\r\n\t\tvar i;\r\n\t\tdt.Columns = [];\r\n\t\tfor(i=0; i<this.Columns.length; i++)\r\n\t\t\tdt.Columns.push([this.Columns[i].Name, this.Columns[i].__type]);\r\n\t\tdt.Rows = [];\r\n\t\tfor(i=0; i<this.Rows.length; i++) {\r\n\t\t\tvar row = [];\r\n\t\t\tfor(var j=0; j<this.Columns.length; j++)\r\n\t\t\t\trow.push(this.Rows[i][this.Columns[j].Name]);\r\n\t\t\tdt.Rows.push(row);\r\n\t\t}\r\n\t\treturn AjaxPro.toJSON(dt);\r\n\t};\r\n\tthis.addRow = function(row) {\r\n\t\tthis.Rows.push(row);\r\n\t};\r\n\tif(c != null) {\r\n\t\tfor(var i=0; i<c.length; i++)\r\n\t\t\tthis.addColumn(c[i][0], c[i][1]);\r\n\t}\r\n\tif(r != null) {\r\n\t\tfor(var y=0; y<r.length; y++) {\r\n\t\t\tvar row = {};\r\n\t\t\tfor(var z=0; z<this.Columns.length && z<r[y].length; z++)\r\n\t\t\t\trow[this.Columns[z].Name] = r[y][z];\r\n\t\t\tthis.addRow(row);\r\n\t\t}\r\n\t}\r\n};\r\n");
        }

        public override string Serialize(object o)
        {
            StringBuilder sb = new StringBuilder();
            this.Serialize(o, sb);
            return sb.ToString();
        }

        public override void Serialize(object o, StringBuilder sb)
        {
            DataTable table = o as DataTable;
            if (table == null)
            {
                throw new NotSupportedException();
            }
            DataColumnCollection columns = table.Columns;
            DataRowCollection rows = table.Rows;
            bool flag = true;
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                sb.Append("new ");
                sb.Append(this.clientType);
                sb.Append("([");
                foreach (DataColumn column in columns)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    sb.Append('[');
                    JavaScriptUtil.QuoteString(column.ColumnName, sb);
                    sb.Append(',');
                    JavaScriptUtil.QuoteString(column.DataType.FullName, sb);
                    sb.Append(']');
                }
                sb.Append("],[");
                flag = true;
                foreach (DataRow row in rows)
                {
                    if (!flag)
                    {
                        sb.Append(",");
                    }
                    JavaScriptSerializer.Serialize(row, sb);
                    flag = false;
                }
                sb.Append("])");
            }
            else
            {
                sb.Append('[');
                foreach (DataRow row in rows)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    sb.Append('{');
                    bool flag2 = true;
                    foreach (DataColumn column in table.Columns)
                    {
                        if (flag2)
                        {
                            flag2 = false;
                        }
                        else
                        {
                            sb.Append(",");
                        }
                        JavaScriptUtil.QuoteString(column.ColumnName, sb);
                        sb.Append(':');
                        JavaScriptSerializer.Serialize(row[column], sb);
                    }
                    sb.Append('}');
                }
                sb.Append(']');
            }
        }
    }
}

